package UILayer;
import CtrLayer.*;

/**
 * LoanUI.
 */
public class LoanUI
{
    private LoanCtr lCtr;
    private Parser parser;

    public  LoanUI(LoanCtr lCtr)
    {
        // initialise instance variables
        this.parser = new Parser();
        this.lCtr = lCtr;
    }

    public void loanMenu()
    {
        boolean exit = false;
        writeMenu();
        while(!exit)
        {
            int choice = parser.scanChoiceInput();
            if(choice == 1) { //Create loan
                createLoan();
                writeMenu();
            }
            if(choice == 2) { //end loan
                endLoan();
                writeMenu();
            }
            if(choice == 3) { //find loan
                findLoan();
                writeMenu();;
            }
            if(choice == 4) { //go back
                exit = true;
            }
        }
    }

    public void writeMenu()
    {
        System.out.println("*****Loan menu*****");
        System.out.println("* (1) create loan *");
        System.out.println("* (2) end loan    *");
        System.out.println("* (3) find loan   *");
        System.out.println("* (4) back        *");
        System.out.println("*******************");
    }

    public void createLoan()
    {
        System.out.println("Enter friends phone number: ");
        String phone = parser.scanInputString();

        System.out.println("Loan id: ");
        String id = parser.scanInputString();

        System.out.println("Date of loan: ");
        String borrowDate = parser.scanInputString();

        System.out.println("Period as int: ");
        int period = parser.scanChoiceInput();

        System.out.println("Serial number of copy: ");
        String serialNumber = parser.scanInputString();
        
        if(lCtr.createLoan(phone, id, borrowDate, period, serialNumber)) {
            lCtr.createLoan(phone, id, borrowDate, period, serialNumber);
        }
        else {
            System.out.println("Couldn't create loan. Either person er copy does not exist");
        }
     }

    public void endLoan()
    {
        System.out.println("Enter friends phone number: ");
        String phone = parser.scanInputString();

        System.out.println("Serial number of copy: ");
        String serialNumber = parser.scanInputString();

        if(lCtr.endLoan(phone, serialNumber) == false)
        {
            System.out.println("Could'nt end loan. Either wrong number or serial");   
        }
        else {
            System.out.println("Copy returned");
        }
    }
    
    public void findLoan()
    {
        System.out.println("Enter loan ID: ");
        String id = parser.scanInputString();
        
        if(lCtr.findLoan(id) != null) {
            lCtr.findLoan(id).printInfo();
        }
        else {
            System.out.println("Could not find information of a loan with id: " + id);        
        }
    }
        
}
